<template>
  <div>
    <div>购物车</div>
    <div v-if="!isLogin">您还没有登录</div>
    <div class="shopcat" v-if="isLogin">
      <div class="shopcat_item" v-for="item in shopcatList" :key="item.id">
        <div class="bookinfo">
          <img :src="item.pic" alt="" />
          <div>
            <p>{{ item.name }}</p>
            <p>{{ item.artist }}</p>
          </div>
        </div>
        <div>
          <span class="presentprice">￥{{ item.presentprice }}</span>
          <el-input-number
            v-model="item.count"
            :min="1"
            :max="999"
            label="描述文字"
            size="small"
          ></el-input-number>
          <el-button
            @click="clearShopcart(item)"
            size="medium"
            type="danger"
            round
          >
            去结算
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import $ from 'jquery'
export default {
  data() {
    return {
      num: 1,
      shopcatList: [],
      isLogin: false
    }
  },
  mounted() {
    // $('.shopcat').niceScroll({ cursorcolor: '#EBEEF5' })
    this.getIsLogin()
  },
  created() {
    this.getShopcat()
  },
  methods: {
    // 点击结算购物车
    async clearShopcart(item) {
      this.$router.push({ name: 'Pay', params: { goodInfo: item } })
      // console.log(item)
      // const shopcatId = item.id

      // // 弹窗询问用户是否结算
      // this.$confirm('此操作将结算该物品, 是否继续?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(async () => {
      //     const {
      //       data: res
      //     } = await this.$http.post(
      //       'http://localhost:3000/api/users/shopcat/delete',
      //       { id: shopcatId }
      //     )
      //     // console.log(res)
      //     if (res.status !== true) {
      //       return this.$message.error('结算失败')
      //     }
      //     this.$message.success('结算成功')
      //     this.getShopcat()
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消结算'
      //     })
      //   })
    },
    // 获取登录状态
    getIsLogin() {
      this.isLogin = sessionStorage.getItem('isLogin')
    },
    // 获取购物车内容
    async getShopcat() {
      const userId = sessionStorage.getItem('user_id')
      const { data: res } = await this.$http.get(
        'http://localhost:3000/api/users/shopcat/' + userId
      )
      this.shopcatList = res.data
      // console.log(res.data)
    }
  }
}
</script>

<style lang="less" scoped>
.shopcat {
  max-height: 780px;
  border-radius: 20px;
  overflow-y: scroll;
  scrollbar-width: none;
  box-shadow: inset 0px 15px 15px -15px rgba(0, 0, 0, 0.3),
    inset 0px -15px 15px -15px rgba(0, 0, 0, 0.3);

  .shopcat_item:nth-last-child(1) {
    margin-bottom: 0px;
  }

  .shopcat_item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px;
    margin-bottom: 10px;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 20px;
    height: 100px;
    // box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

    .presentprice {
      margin-right: 20px;
      color: red;
      font-size: 18px;
      font-weight: 600;
    }

    .bookinfo {
      display: flex;
      align-items: center;

      > img {
        margin-right: 20px;
        border-radius: 20px;
        height: 100px;
      }

      p:nth-child(1) {
        font-size: 20px;
        font-weight: 600;
      }
      p:nth-child(2) {
        font-size: 14px;
      }
    }
  }
}
</style>
